// __PUBLIC__/js/page/admin/user/nodemanage
require(['../../common'], function() {
  require(['user.nodemanage'])
})

define('user.nodemanage', [
  'jquery',
  'page/admin/user/nodemanager/ytree',
  'jqp/validate'
], function($, ytree) {
  'use strict';

  var tree = {
    init: function(data) {
      var $cates = $('#J_tree')
      this.$cates = $cates
      this.tree = ytree($cates, {
        data: data
      })

      this.$name = $('#name')
      this.$title = $('#title')
      this.$status = $('input[name="status"]')
      this._bind()
    },
    _bind: function() {
      this.$cates
      .on('click', '.a', $.proxy(function(e) {
          this._open(e.target)
        e.preventDefault()
      }, this))
      .on('mouseover', 'li', function(e) {
        $('')
      })
     .on('click', '.oc', $.proxy(function(e) {
            this._toggle(e.target)
        }, this))

    },
    _set: function(id) {
      $.getJSON(APP + '?app=admin&m=User&a=getNodeInfo', {id : id})
      .done(function(data) {
        if (data !== 0) {
          $("#name").val(data['name'])
          $("#title").val(data['title'])
          if (data['status'] == 1) {
            $("input[type=radio][value=1]").attr("checked", 'checked');
          } else if (data['status'] == 0) {
            $("input[type=radio][value=0]").attr("checked", 'checked');
          }
          $("#vid").val(data['id']);
        }
      })
    },
    _edit: function(elem) {
      var $parent = $(elem).parent()
      var active = 'active'
      if ($parent.hasClass(active)) {
        return false
      }
      var model = this.tree.model
    },
    _open: function (e){
    	var  id=$(e).attr('data')
    	this._set(id)
    } ,
    _toggle: function(elem) {
      $(elem).parent().toggleClass('open')
    }
  }
  $.getJSON(APP + '?app=admin&m=User&a=getJsonNodeTree')
    .done(function(data) {
      tree.init(data)
    })
    .fail(function() {
      throw new Error('get json failed')
    })



  $('#J_form').validate({
    classPlace: '.kv',
    errorPlace: '.kv-error',
    errors: {
      name: {
        required: '请填写节点名称',
      },
      title: {
        required: '请填写节点昵称',
      },
    }
  })


})
